home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5538 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  83 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pre-processing:  Can this be done?
  5. Date: 08 Feb 1996 20:16:52 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb8131652@qcd.lanl.gov>
  8. References: <DMGo4t.7r7@gti-ia.nl>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: paulw@gti-ia.nl's message of Thu, 8 Feb 1996 14:26:52 GMT
  13.  
  14. In article <DMGo4t.7r7@gti-ia.nl> paulw@gti-ia.nl (Paul Wallis)
  15. writes: 
  16. <snip>
  17.    I'm trying to write a function which will print trace statements.
  18.    I want the file and line to be added to the argument list
  19.    automatically.  I am, however, having some problems working
  20.    my way around the pre-processor. I would like to be able to enter
  21.    the following line:
  22.  
  23.        trace("%d, %f", an_int, a_float);
  24.  
  25.    and have the arguments passed as so:
  26.  
  27.        _trace(__FILE__, __LINE__, "%d, %f", an_int, a_float);
  28.  
  29. There are no varargs macros in portable C.
  30.  
  31.    at the x.  Apparently not liking multiple arguments.  Is there
  32.    any way to get it to accept this, without having to do this,
  33.    which is very ugly:
  34.  
  35.        trace(("%d, %f", an_int, a_float));
  36.  
  37. This is usually the preferred solution.
  38.  
  39.    I have also tried redefining a macro as such:
  40.  
  41.        #define trace( _trace(__FILE__, __LINE__
  42.  
  43.    but to no avail.  Is there an answer, or am I stuck with the
  44.    ugly?
  45.  
  46. This, as you discovered, does not work. You either have object like
  47. macros, or function like macros: no comlicated constructs.
  48.  
  49.    Thanks in advance for any help,
  50.  
  51. If you want to trade one ugliness for another, you may use the
  52. following:
  53.  
  54. extern void record(const char*,int);
  55. #define trace record(__FILE__,__LINE__); trace
  56.  
  57.  
  58. and in another file 
  59.  
  60. static const char *file;
  61. static int line;
  62.  
  63. void record(const char *lastfile, int lastline) {
  64.   file=lastfile; /* We are assuming we do not need to copy string */
  65.   line=lastline;
  66. }
  67.  
  68. void trace(const char *format, ...) {
  69.  /* uses the file scope variables file and line */
  70.  file=NULL; line=0; /* to catch cases where trace directly called
  71.                        instead of through macro */
  72. }
  73.  
  74. Cheers
  75. Tanmoy
  76. --
  77. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  78. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  79. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  80. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  81. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  82. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  83.